package tw.edu.tku.im.maze;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;


public class PlayerInfo {
   String userID;
   String name;
   int lv;
   int exp;
   int wins;
   int losses;
   int leaver;
   int ticket;
   
   public static int freeTicket() {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String dateString = sdf.format(new Date());
      return dateString.hashCode();
   }
   
   public static PlayerInfo parse(JSONObject playerInfo) throws JSONException {
      String userID = playerInfo.getString("userID");
      String name = playerInfo.getString("name");
      int parmeter[] = new int[6];
      parmeter[0] = playerInfo.getInt("lv");
      parmeter[1] = playerInfo.getInt("exp");
      parmeter[2] = playerInfo.getInt("wins");
      parmeter[3] = playerInfo.getInt("losses");
      parmeter[4] = playerInfo.getInt("leaver");
      parmeter[5] = playerInfo.getInt("ticket");
      return new PlayerInfo(userID, name, parmeter);
   }
   
   public PlayerInfo(String userID) {
      this(userID, userID);
   }   
   
   public PlayerInfo(String userID, String name) {
      this.userID = userID;
      this.name = name;
      this.lv = 1;
      this.exp = 0;
      this.wins = 0;
      this.losses = 0;
      this.leaver = 0;
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String dateString = sdf.format(new Date());
      this.ticket = dateString.hashCode();
   }
   
   public PlayerInfo(String userID, String name, int ticket) {
      this(userID, name, 1, 0, 0, 0, 0, ticket);
   }
   
   public PlayerInfo(String userID, String name, int[] parameter) {
      this.userID = userID;
      this.name = name;
      if(parameter.length!=6) throw new IllegalArgumentException();
      this.lv = parameter[0];
      this.exp = parameter[1];
      this.wins = parameter[2];
      this.losses = parameter[3];
      this.leaver = parameter[4];
      this.ticket = parameter[5];
   }
   
   public PlayerInfo(String userID, String name, int lv, int exp, int wins, 
                     int losses, int leaver, int ticket) 
   {
      this.userID = userID;
      this.name = name;
      this.lv = lv;
      this.exp = exp;
      this.wins = wins;
      this.losses = losses;
      this.leaver = leaver;
      this.ticket = ticket;
   }
   
   public String getUserID() {
      return this.userID;
   }
   
   public void setUserID(String userID) {
      this.userID = userID;
   }
   
   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      return this.name;
   }
   
   public void setLv(int lv) {
      this.lv = lv;
   }
   public int getLv() {
      return this.lv;
   }
   
   public void setExp(int exp) {
      this.exp = exp;
   }
   public int getExp() {
      return this.exp;
   }
   
   public void setWins(int wins) {
      this.wins = wins;
   }
   public int getWins() {
      return this.wins;
   }
   
   public void setLosses(int losses) {
      this.losses = losses;
   }
   public int getLosses() {
      return this.losses;
   }
   
   public void setLeaver(int leaver) {
      this.leaver = leaver;
   }
   public int getLeaver() {
      return this.leaver;
   }
   
   public void setTicket(int ticket) {
      this.ticket = ticket;
   }
   public int getTicket() {
      return this.ticket;
   }
   
   public String toString() {
      StringBuilder ans = new StringBuilder("{ \"userID\" : \"");
      ans.append(this.userID).append("\", \"name\" : \"")
         .append(this.name).append("\", \"lv\" : ")
         .append(this.lv).append(", \"exp\" : ")
         .append(this.exp).append(", \"wins\" : ")
         .append(this.wins).append(", \"losses\" : ")
         .append(this.losses).append(", \"leaver\" : ")
         .append(this.leaver).append(", \"ticket\" : ")
         .append(this.ticket).append(" }");
      return ans.toString();
   }
   
   public String toJSONString() throws JSONException {
      JSONObject playerInfo = new JSONObject();
      playerInfo.put("userID", this.userID);
      playerInfo.put("name", this.name);
      playerInfo.put("lv", this.lv);
      playerInfo.put("exp", this.exp);
      playerInfo.put("wins", this.wins);
      playerInfo.put("losses", this.losses);
      playerInfo.put("leaver", this.leaver);
      playerInfo.put("ticket", this.ticket);
      return playerInfo.toString();
   }
}
